<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * get请求
 * @param $url ,请求地址
 * @return bool|string
 */

if (!function_exists('getRequest')) {

    function getRequest($url, $isParseResultJson = true, $json2Array = false)
    {
        $headerArray = array("Content-type:application/json;", "Accept:application/json");
        $ch = curl_init();
        //$url =  urlencode($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        if ($output && $isParseResultJson) {
            // $isParseResultJson2Obj = ture, 结果为array
            $output = json_decode($output, $json2Array);
        }
        return $output;
    }
}

/**
 * post请求
 * @param $url ,请求地址
 * @param $data ,网络请求参数,进行过decode的编码的json数组
 * @return bool|string
 */
if (!function_exists('postRequestJson')) {

    function postRequestJson($url, $data, $isParseResultJson = true, $json2Array = false)
    {
        $data = json_encode($data);
        $headerArray = array("Content-type:application/json;charset='utf-8'", "Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        //将返回的json对象解码成数组对象并返回
        if ($output && $isParseResultJson) {
            // $isParseResultJson2Obj = ture, 结果为array
            $output = json_decode($output, $json2Array);
        }

        return $output;
    }
}

if (!function_exists('postRequest')) {

    function postRequest($url, $data, $isParseResultJson = true, $json2Array = false)
    {
        $headerArray = array("Accept:application/json");
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($curl);
        curl_close($curl);
        //将返回的json对象解码成数组对象并返回
        if ($output && $isParseResultJson) {
            // $isParseResultJson2Obj = ture, 结果为array
            $output = json_decode($output, $json2Array);
        }
        return $output;
    }
}

/**
 * CURL下载文件 成功返回文件名，失败返回false
 * @param $url
 * @param string $savePath
 * @return bool|string
 * @author Zou Yiliang
 */
if (!function_exists('downFile')) {

    function downFile($url, $savePath = './uploads', $fileName)
    {
        //$url = 'http://www.baidu.com/img/bdlogo.png';
        /*
        HTTP/1.1 200 OK
        Connection: close
        Content-Type: image/jpeg
        Content-disposition: attachment; filename="cK4q4fLsp7YOlaqxluDOafB.jpg"
        Date: Sun, 18 Jan 2015 16:56:32 GMT
        Cache-Control: no-cache, must-revalidate
        Content-Length: 963704
        */
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);  //需要response header
        curl_setopt($ch, CURLOPT_NOBODY, FALSE);  //需要response body
        $response = curl_exec($ch);

        //分离header与body
        $header = '';
        $body = '';
        if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == '200') {
            $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); //头信息size
            $header = substr($response, 0, $headerSize);
            $body = substr($response, $headerSize);
        }
        curl_close($ch);
        //文件名

        $fullName = rtrim($savePath, '/') . '/' . $fileName;
        //创建目录并设置权限
        $basePath = dirname($fullName);
        if (!file_exists($basePath)) {
            @mkdir($basePath, 0777, true);
            @chmod($basePath, 0777);
        }

        if (file_put_contents($fullName, $body)) {
            return $fullName;
        }

        return false;
    }
}

/**
 * 02
 * 获取PDF的页数
 * 03
 */

if (!function_exists('getPDFPageTotal')) {
    function getPDFPageTotal($path)
    {
        // 打开文件
        if (!$fp = @fopen($path, "r")) {
            \utils\LogUtils::error("打开文件{$path}失败");
            return false;
        } else {
            $max = 0;
            while (!feof($fp)) {
                $line = fgets($fp, 255);
                if (preg_match('/\/Count [0-9]+/', $line, $matches)) {
                    preg_match('/[0-9]+/', $matches[0], $matches2);
                    if ($max < $matches2[0]) $max = $matches2[0];
                }
            }
            fclose($fp);
            // 返回页数
            return $max;
        }
    }
}

if (!function_exists('getFileSavePath')) {
    function getFileSavePath()
    {
        return \think\facade\Env::get('ROOT_PATH') . 'public/file';
    }
}


if (!function_exists('getFileROOT')) {
    function getFileROOT()
    {
        return \think\facade\Env::get('ROOT_PATH');
    }
}

if (!function_exists('isHaveData')) {
    function isHaveData($array)
    {
        return $array && is_array($array) && count($array)>0;
    }
}








